package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import android.util.Log;

import com.google.gson.Gson;
import sz.szsmk.citizencard.model.network.dzbean.req.BillRequest;
import sz.szsmk.citizencard.model.network.dzbean.res.BillResponse;
import sz.szsmk.citizencard.mvp.BillMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;

import javax.inject.Inject;

import rx.functions.Action1;

/**
 * Created by yangzhengwen on 2017/9/30.
 */

public class BillPresenter extends AppBasePresenter<BillMvpView> {

    @Inject
    NetworkDateSource mNetworkDateSource;

    @Inject
    public BillPresenter() {
        getComponent().inject(this);
    }

    public void getBillList(BillRequest billRequest) {
        checkViewAttached();
        ObjectSignatureUtil.signatureRequest(billRequest);
        mNetworkDateSource.getBillList(billRequest, new Action1<BillResponse>() {
            @Override
            public void call(BillResponse baseRes) {
                String code = baseRes.getCode();
                if (code.equals("0000")){
                    String json = new Gson().toJson(baseRes);
                    BillResponse tradesResponse = new Gson().fromJson(json, BillResponse.class);
                    BillMvpView mvpView = getMvpView();
//                    getMvpView().onGetBillFail();
                    mvpView.onGetBillSuccess(baseRes);
                }else {
                    getMvpView().onGetBillFail();
                }

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                Log.i("error",throwable.getMessage());
            }
        });
    }
}
